/*
 * HtmlComparisonDataTable.java
 * 
 * Created on 2007.06.20, 22:59:09
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hr.ctg.component.ui;

import hr.ctg.domain.model.Objekt;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 * 
 * @author Administrator
 */
public class HtmlComparisonDataTable extends UIComponentBase {

	String tableHeader;
	String tableFooter;
	String headerStyle;
	String headerStyleClass;
	String footerStyle;
	String footerStyleClass;
	String tableHeaderStyle;
	String tableHeaderStyleClass;
	String tableFooterStyle;
	String tableFooterStyleClass;
	String leadingColumnStyle;
	String leadingColumnStyleClass;
	String rowStyleClasses;

	// String column;

	public HtmlComparisonDataTable() {
	}

	@Override
	public String getFamily() {
		return null;
	}

	@Override
	@SuppressWarnings("unchecked")
	public void encodeBegin(final FacesContext facesCtx) throws IOException {
		int columnCount = 0;
		int rowStylesCounter = 0;

		final Map comparisonTable = (Map) getAttributes().get("osobine");
		final List objekts = (List) getAttributes().get("objekti");
		if (null == tableHeader) {
			tableHeader = (String) getAttributes().get("tableHeader");
		}
		String columnOsobine = getAttributes().get("column").toString();
		columnCount = objekts.size() + 1;

		List rowSC = new ArrayList<String>();
		if (null != rowStyleClasses) {
			StringTokenizer st = new StringTokenizer(rowStyleClasses);
			while (st.hasMoreTokens()) {
				rowSC.add(st.nextToken());
			}
		} else {
			System.out.println("rowStyleClasses nije definiran");
		}

		final ResponseWriter rw = facesCtx.getResponseWriter();

		// Set<Integer> osobinaIdsSet = comparisonTable.keySet();
		final Set<Map.Entry<Integer, List<String>>> osobineSet = comparisonTable
				.entrySet();
		// final Set<Map.Entry<Integer, Objekt>> objEntries =
		// objekts.entrySet();

		rw.startElement("table", this);
		// table header
		if (null != this.getFacet("header")) {
			rw.startElement("tr", this);
			rw.writeAttribute("style", tableHeaderStyle, null);
			rw.writeAttribute("class", tableHeaderStyleClass, null);
			rw.startElement("th", this);
			rw.endElement("th");
			rw.startElement("th", this);
			rw.writeAttribute("colspan", columnCount - 1, null);
			this.getFacet("header").encodeBegin(facesCtx);
			rw.endElement("th");
			rw.endElement("tr");
		}

		// column headers
		rw.startElement("tr", this);
		rw.writeAttribute("style", headerStyle, null);
		rw.writeAttribute("class", headerStyleClass, null);
		rw.startElement("th", this);
		rw.writeAttribute("style", leadingColumnStyle, null);
		rw.writeAttribute("class", leadingColumnStyleClass, null);
		rw.writeText(columnOsobine, null);
		rw.endElement("th");

		for (final Iterator<Objekt> objIt = objekts.iterator(); objIt.hasNext();) {
			rw.startElement("th", this);
			rw.writeText(objIt.next().getNaziv(), null);
			rw.endElement("th");
		}
		rw.endElement("tr");

		// column data

		for (final Iterator<Map.Entry<Integer, List<String>>> it = osobineSet
				.iterator(); it.hasNext();) {
			final Map.Entry<Integer, List<String>> entry = it.next();
			rw.startElement("tr", this);
			if (rowSC.size() > 0) {
				rw.writeAttribute("class", rowSC.get(rowStylesCounter++
						% rowSC.size()), null);
			}
			rw.startElement("td", this);
			rw.writeAttribute("style", leadingColumnStyle, null);
			rw.writeAttribute("class", leadingColumnStyleClass, null);
			rw.writeText(entry.getKey(), null);
			rw.endElement("td");
			for (final Iterator<String> oss = entry.getValue().iterator(); oss
					.hasNext();) {
				rw.startElement("td", this);
				rw.writeText(oss.next(), null);
				rw.endElement("td");
			}
			rw.endElement("tr");
		}
		// table footer
		if (null != this.getFacet("footer")) {
			rw.startElement("tr", this);
			rw.writeAttribute("class", tableFooterStyleClass, null);
			rw.writeAttribute("style", tableFooterStyle, null);
			rw.startElement("td", this);
			rw.writeAttribute("colspan", columnCount, null);
			this.getFacet("footer").encodeBegin(facesCtx);
			rw.endElement("td");
			rw.endElement("tr");
		}
		rw.endElement("table");

	}

	public String getHeaderStyle() {
		return headerStyle;
	}

	public void setHeaderStyle(String headerStyle) {
		this.headerStyle = headerStyle;
	}

	public String getHeaderStyleClass() {
		return headerStyleClass;
	}

	public void setHeaderStyleClass(String headerStyleClass) {
		this.headerStyleClass = headerStyleClass;
	}

	public String getFooterStyle() {
		return footerStyle;
	}

	public void setFooterStyle(String footerStyle) {
		this.footerStyle = footerStyle;
	}

	public String getFooterStyleClass() {
		return footerStyleClass;
	}

	public void setFooterStyleClass(String footerStyleClass) {
		this.footerStyleClass = footerStyleClass;
	}

	public String getTableHeaderStyle() {
		return tableHeaderStyle;
	}

	public void setTableHeaderStyle(String tableHeaderStyle) {
		this.tableHeaderStyle = tableHeaderStyle;
	}

	public String getTableHeaderStyleClass() {
		return tableHeaderStyleClass;
	}

	public void setTableHeaderStyleClass(String tableHeaderStyleClass) {
		this.tableHeaderStyleClass = tableHeaderStyleClass;
	}

	public String getTableFooterStyle() {
		return tableFooterStyle;
	}

	public void setTableFooterStyle(String tableFooterStyle) {
		this.tableFooterStyle = tableFooterStyle;
	}

	public String getTableFooterStyleClass() {
		return tableFooterStyleClass;
	}

	public void setTableFooterStyleClass(String tableFooterStyleClass) {
		this.tableFooterStyleClass = tableFooterStyleClass;
	}

	public String getLeadingColumnStyle() {
		return leadingColumnStyle;
	}

	public void setLeadingColumnStyle(String leadingColumnStyle) {
		this.leadingColumnStyle = leadingColumnStyle;
	}

	public String getLeadingColumnStyleClass() {
		return leadingColumnStyleClass;
	}

	public void setLeadingColumnStyleClass(String leadingColumnStyleClass) {
		this.leadingColumnStyleClass = leadingColumnStyleClass;
	}

	public String getRowStyleClasses() {
		return rowStyleClasses;
	}

	public void setRowStyleClasses(String rowStyleClasses) {
		this.rowStyleClasses = rowStyleClasses;
	}

	public String getTableHeader() {
		return tableHeader;
	}

	public void setTableHeader(String tableHeader) {
		this.tableHeader = tableHeader;
	}

	public String getTableFooter() {
		return tableFooter;
	}

	public void setTableFooter(String tableFooter) {
		this.tableFooter = tableFooter;
	}

	/*
	 * public String getColumn() { return column; }
	 * 
	 * public void setColumn(String column) { this.column = column; }
	 */

}
